import cTitle from "components/title";
import { Toast } from 'vant';

export default {
  data() {
    return {
      transferID: "",
      transferIntegral: "",
      integral: 0,
      name: "",
      nick_name:""
    };
  },
  activated() {
    this.initData();
    this.getName();
    this.getData();
  },
  methods: {
    serach_nick_name() {
      if (this.transferID == '') {
          return
      }
      if (this.timerId) { clearTimeout(this.timerId) }
      this.timerId = setTimeout(() => {
          this.getNickName()
      },300)
    },
    getNickName(){
      $http
      .get('member.member.memberInfo', {uid:this.transferID}, 'loading')
      .then(response => {
        console.log(response)
        if (response.result === 1) {
          this.nick_name = response.data.nickname;
        } else {
          // Toast(response.msg);
          this.nick_name = '';
        }
      })
      .catch(error => {
        console.log(error);
      });
    },
    getName() {
      let url = "plugin.integral.Frontend.Controllers.page.get-name";
      if(this.$route.query.plugin_name == 'new_integral'){
        url = "plugin.new-integral.Frontend.Controllers.page.get-name";
      }
      $http
        .get(url, {}, " ")
        .then(response => {
          if (response.result === 1) {
            this.name = response.data;
            console.log(response)
          } else {
            Toast(response.msg);
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    initData() {
      this.transferID = "";
      this.transferIntegral = "";
      this.integral = 0;
      this.name = "";
      this.nick_name=""
    },
    getData() {
      let url = "plugin.integral.Frontend.Controllers.Page.index";
      if(this.$route.query.plugin_name == 'new_integral'){
        url = 'plugin.new-integral.Frontend.Controllers.Page.index';
      }
      $http.get(url, {}, " ")
        .then(response => {
          if (response.result === 1) {
            this.transferID = "";
            this.transferIntegral = "";
            this.integral = response.data.member_integral;
          } else {
            Toast(response.msg);
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    sureTransfer() {
      if (!this.transferIntegral) {
        Toast("请输入转让数额");
        return;
      }
      if (Number(this.transferIntegral) > Number(this.integral)) {
        Toast(`输入的${this.fun.initWithdrawal()}数额超过已有的` + this.name);
        return;
      }
      let url = "plugin.integral.Frontend.Modules.Integral.Controllers.Transfer";
      if(this.$route.query.plugin_name == 'new_integral'){
        url = "plugin.new-integral.Frontend.Modules.Integral.Controllers.Transfer";
      }
      $http
        .get(
          url,
          {
            change_value: this.transferIntegral,
            recipient_id: this.transferID
          },
          "转让中"
        )
        .then(response => {
          if (response.result === 1) {
            this.getData();
            if(this.$route.query.plugin_name == 'new_integral'){
              this.$router.push(this.fun.getUrl('Integral_love_detail',{},{plugin_name:'new_integral'}));
            }else{
              this.$router.push(this.fun.getUrl('Integral_love_detail'));
            }
            Toast("转让成功");
          } else {
            Toast(response.msg);
          }
        })
        .catch(error => {
          console.log(error);
        });
    }
  },
  components: { cTitle }
};
